﻿using CloudMusic.Helper;
using CloudMusic.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Threading;

namespace CloudMusic.ViewModel
{
    public class PageAlbumInfoViewModel : MusicViewModelBase
    {

        public PageAlbumInfoViewModel()
        {
        
        }
        private Visibility subVIS = Visibility.Collapsed;

        /// <summary>
        /// 专辑音乐列表
        /// </summary>
        private ObservableCollection<MusicModel> albumSongList;
        public ObservableCollection<MusicModel> AlbumSongList
        {
            get
            {
                if (albumSongList == null)
                {
                    albumSongList = new ObservableCollection<MusicModel>();
                }
                return albumSongList;
            }
            set
            {
                albumSongList = value;
                //MainMusicList = songSheetList;
                OnPropertyChanged("AlbumSongList");
            }
        }
        #region 专辑信息
        /// <summary>
        /// 评论数量
        /// </summary>
        private string commentCount;
        public string CommentCount
        {
            get { return commentCount; }
            set { commentCount = value; OnPropertyChanged("CommentCount"); }
        }
        /// <summary>
        /// 专辑名字全称
        /// </summary>
        private string albumName;
        public string AlbumName
        {
            get { return albumName; }
            set { albumName = value; OnPropertyChanged("AlbumName"); }
        }
        /// <summary>
        /// 歌手
        /// </summary>
        private string singerName;
        public string SingerName
        {
            get { return singerName; }
            set { singerName = value; OnPropertyChanged("SingerName"); }
        }
        /// <summary>
        /// 时间
        /// </summary>
        private string creatTime;
        public string CreatTime
        {
            get { return creatTime; }
            set { creatTime = value; OnPropertyChanged("CreatTime"); }
        }
        /// <summary>
        /// 分享
        /// </summary>
        private string shareCount;
        public string ShareCount
        {
            get { return shareCount; }
            set { shareCount = value; OnPropertyChanged("ShareCount"); }
        }
        /// <summary>
        /// 收藏数量
        /// </summary>
        private string subCount;
        public string SubCount
        {
            get { return subCount; }
            set { subCount = value; OnPropertyChanged("SubCount"); }
        }
        /// <summary>
        /// 秒速
        /// </summary>
        private string description;
        public string Description
        {
            get { return description; }
            set { description = value; OnPropertyChanged("Description"); }
        }
        /// <summary>
        /// 封面
        /// </summary>
        private string picUrl;
        public string PicUrl
        {
            get { return picUrl; }
            set { picUrl = value; OnPropertyChanged("PicUrl"); }
        }
        /// <summary>
        /// 收藏与否
        /// </summary>
        private bool liked;
        public bool Liked
        {
            get { return liked; }
            set { liked = value; OnPropertyChanged("Liked"); }
        }
        #endregion 
        public PageAlbumInfoViewModel(long id)
        {
            try
            {
                Task.Run(async () =>
                {
                    await Task.Run(() =>
                    {
                        var strJosn = CommonHelper.Get("http://localhost:3000/album/detail/dynamic?id=" + id);
                        JObject obj = (JObject)JsonConvert.DeserializeObject(strJosn);
                        var albumJson = CommonHelper.Get("http://localhost:3000/album?id=" + id);
                        JObject alubmObj = (JObject)JsonConvert.DeserializeObject(albumJson);
                        string alubm = alubmObj["album"].ToString();
                        string song = alubmObj["songs"].ToString();
                        JObject Data = (JObject)JsonConvert.DeserializeObject(alubm);
                        string artists = Data["artists"][0].ToString();
                        JObject artistsData = (JObject)JsonConvert.DeserializeObject(artists);
                        string info = Data["info"].ToString();
                        JObject infoData = (JObject)JsonConvert.DeserializeObject(info);
                        CommentCount = "评论(" + obj["commentCount"].ToString() + ")";
                        ShareCount = obj["shareCount"].ToString();
                        SubCount = obj["subCount"].ToString();
                        string alias = Data["alias"].ToString();
                        //JObject aliaData = (JObject)JsonConvert.DeserializeObject(alias);
                        string alia = alias.ToString().Replace('[', ' ').Replace(']', ' ').Replace('"',' ').Replace('"',' ').Trim();
                        if (alias != "[]")
                        {
                            AlbumName = Data["name"].ToString() + "(" + alia + ")";
                        }
                        else
                        {
                            AlbumName = Data["name"].ToString();
                        }
                        SingerName = artistsData["name"].ToString();
                        // AlbumName = Data["name"].ToString() + "(" + Data["alias"].ToString() + ")";
                        Description = Data["description"].ToString();
                        CreatTime = DateTimeHelper.GetDateToString(Data["publishTime"].ToString()).ToString("yyyy-MM-dd");
                        Liked = (bool)infoData["liked"];
                        PicUrl = Data["picUrl"].ToString();
                        List<AlbumInfoModel> list = JsonConvert.DeserializeObject<List<AlbumInfoModel>>(song); //将json数据转化为对象类型并赋值给list
                        for (int i = 0; i < list.Count; i++)
                        {
                            MusicModel data = new MusicModel
                            {
                                Index = i < 9 ? "0" + (i + 1).ToString() : i.ToString(),
                                Album = list[i].al.name,
                                AlbumId = list[i].al.id,
                                Name = list[i].name,
                                Subtitle = GetSubtitle(list[i].alia),
                                Id = list[i].id,
                                CoverTitle = CommonHelper.GetTitle(list[i].name, list[i].alia),
                                Artist = list[i].ar[0].name,
                                SingerId = list[i].ar[0].id,
                                Type = 1,
                                PicUrl = list[i].al.picUrl,
                                SongId = id,
                                CanPlay = true,
                                TimeStr = DateTimeHelper.GetTime((list[i].dt / 1000).ToString()),
                                MVVis = CommonHelper.GetMvVis(list[i].mv),
                                VIPVis = CommonHelper.GetVis(list[i].privilege.fee),
                                STVis = CommonHelper.GetVis(list[i].privilege.fee),
                                SQVis = CommonHelper.GetVis(list[i].privilege.maxbr),
                                Source = list[i].al.name,
                                Pop = list[i].pop.ToString(),
                                ChargeInfoList = EntityHelper.GetChargeInfos(list[i].privilege.chargeInfoList),
                                SubtitleVis = subVIS,
                                Playing=false,
                                Like = CommonHelper.CheckLikes(list[i].id)
                            };
                            ThreadPool.QueueUserWorkItem(delegate
                            {
                                SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext(Application.Current.Dispatcher));
                                SynchronizationContext.Current.Post(pl =>
                                {
                                    AlbumSongList.Add(data);
                                }, null);
                            });
                        }
                    });
                });
            }
            catch (System.Exception) { }
        }

        private string GetSubtitle(List<string> str)
        {
            subVIS = Visibility.Collapsed;
            string al = null;
            if (str.Count > 0)
            {
                for (int i = 0; i < str.Count; i++)
                {
                    al += "(" + str[i] + ")";
                }
                subVIS = Visibility.Visible;
            }
            return al;
        }
    }
}
